﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Net.Mail;

namespace AppGauge
{
    /// <summary>
    /// APUT provides AppGauge Processor Utilities like Random No Generation, etc.
    /// All Functions are Static.
    /// </summary>
    public class APUT
    {
        /// <summary>
        /// Generates a Random string of Numbers.
        /// </summary>
        /// <param name="StringLength">Length of the string. String Length should be between 2 and 10.</param>
        /// <returns>Random String of Provided Length.</returns>
        /// <exception cref="System.Exception">String Length must be more than 2 and less than 10 characters</exception>
        public static string RandomNumbers(int StringLength)
        {
            if (StringLength < 3 || StringLength > 10)
                throw new Exception("String Length must be more than 2 and less than 10 characters");

            string str = "";
            Random r = new Random();

            for (int i = 0; i < StringLength; i++)
            {
                str += r.Next(2, 688).ToString();
            }

            return str;
        }

        /// <summary>
        /// Sends a HTML Formatted Email.
        /// </summary>
        /// <param name="To">To.</param>
        /// <param name="From">From.</param>
        /// <param name="Subject">The subject.</param>
        /// <param name="Message">The message.</param>
        /// <returns>True on success</returns>
        public static bool SendMail(string To, string From, string Subject, string Message)
        {
            using (MailMessage msg = new MailMessage(From, To))
            {
                msg.Subject = Subject;
                msg.Body = Message;
                msg.IsBodyHtml = true;

                SmtpClient email = new SmtpClient();

                try
                {
                    email.Host = "localhost";
                    email.Port = 25;

                    email.Send(msg);
                    return true;
                }
                catch (SmtpException se)
                {
                    return false;
                }

            }
        }


        /// <summary>
        /// Converts the data table to string.
        /// </summary>
        /// <param name="dataTable">The data table.</param>
        /// <returns>String</returns>
        public static string Dt2S(DataTable dataTable)
        {
            var output = new StringBuilder();

            var columnsWidths = new int[dataTable.Columns.Count];

            // Get column widths
            foreach (DataRow row in dataTable.Rows)
            {
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    var length = row[i].ToString().Length;
                    if (columnsWidths[i] < length)
                        columnsWidths[i] = length;
                }
            }

            // Get Column Titles
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                var length = dataTable.Columns[i].ColumnName.Length;
                if (columnsWidths[i] < length)
                    columnsWidths[i] = length;
            }

            // Write Column titles
            for (int i = 0; i < dataTable.Columns.Count; i++)
            {
                var text = dataTable.Columns[i].ColumnName;
                output.Append("|" + PadCenter(text, columnsWidths[i] + 2));
            }
            output.Append("|\n" + new string('=', output.Length) + "\n");

            // Write Rows
            foreach (DataRow row in dataTable.Rows)
            {
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    var text = row[i].ToString();
                    output.Append("|" + PadCenter(text, columnsWidths[i] + 2));
                }
                output.Append("|\n");
            }
            return output.ToString();
        }

        private static string PadCenter(string text, int maxLength)
        {
            int diff = maxLength - text.Length;
            return new string(' ', diff / 2) + text + new string(' ', (int)(diff / 2.0 + 0.5));

        }
    }

}
